package com.neusoft.filter;

import com.alibaba.nacos.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Slf4j
//@Component
public class AuthGatewayFilterFactory implements GlobalFilter{
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("鉴权开始");
        String token=exchange.getRequest().getQueryParams().getFirst("token");
        log.info("客户端token："+token);
        //假设用户实际的TOKEN应该是admin
        if(!StringUtils.equals(token,"admin")){
            log.info("客户端token错误");
            //响应HTTP状态码（401：没有访问权限）
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            //请求结束
            return exchange.getResponse().setComplete();
        }
        log.info("客户端token正确,鉴权成功！");
        //继续执行过滤器链中的下一个资源
        return chain.filter(exchange);
    }
}
